// pages/myPage/myPage.js
var app = getApp();
Page({
    data: {
        code: "获取验证码",
        loginAuthor: false,
        perfect: false,
        last_time: '',
        is_show: true,
        is_login: true,
        member: "",
        order: "",
        id: "",
        is_authorization:true,
      flag:true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      wx.getSetting({
        success: (res) => {
          if (typeof res.authSetting['scope.userInfo'] === 'undefined') {
            this.setData({
              flag: false,
            })
          } 
          /*
            * res.authSetting = {
            *   "scope.userInfo": true,
            *   "scope.userLocation": true
            * }
            */
        }
      })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        that.init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },//初始化页面
    init: function () {
        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success: function (res) {
                if (res.errMsg == "getStorage:ok") {
                    var nickName = res.data.nickName;
                    var avatarUrl = res.data.avatarUrl;
                    var open_id = res.data.open_id;
                    that.setData({
                        memberInfo: res.data,
                        loginAuthor: false,
                        nickName: nickName,
                        avatarUrl: avatarUrl,
                        perfect: true
                    })
                }
            },
            fail: function () {
                that.setData({
                    is_login:false
                })
            }
        })
        // 检验登陆状态
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                var open_id=data.open_id;
                if (member_id && open_id) {
                    that.setData({
                        is_login: true,
                        member_id: member_id,
                        is_authorization: true
                    })
                    that.get_use_info(member_id);
                } else if (!member_id && open_id){
                    that.setData({
                        is_login: false,
                        is_authorization: false
                    })
                }
            }
        })
    },//获取用户信息
    get_use_info: function (member_id) {
        var that = this;
        var params = {};
        params['id'] = member_id;
        wx.request({
            url: app.config() + 'member/xiaoMemberInfo',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                wx.hideLoading();
                res.data.data.member['defult_head_img'] = "../../images/tx.jpg"
                that.setData({
                    member: res.data.data.member,
                    order: res.data.data.order
                })
            }
        })
    },
    redirect_to: function (e) {
        var that = this;
        var id = that.data.member_id;
        var url_address = e.currentTarget.dataset.title;
        var point = e.currentTarget.dataset.point;
        if (url_address == 'integral') {
            var page_url_address = url_address + '?integral=' + point;
        } else if (url_address == 'coupon') {
            var page_url_address = url_address + '?flag=center';
        } else if (url_address == 'VolumeData' || url_address == 'address') {
            var page_url_address = url_address;
        } else if (url_address == 'wattingPayment') {
            var page_url_address = url_address;
        } else if (url_address == 'wattingReceipt') {
            var page_url_address = url_address;
        } else if (url_address == 'completeOrder') {
            var page_url_address = url_address;
        } else if (url_address == 'refund') {
            var page_url_address = url_address;
        }
        if (id) {
            wx.navigateTo({
                url: '../../pages/' + url_address + '/' + page_url_address,
            })
        } else {
            that.manual_login();
        }
    },//手动点击登陆
    manual_login: function () {
        var that = this;
        var member_id = that.data.member_id;
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo'] == true) {
                    if (!member_id) {
                        wx.navigateTo({
                            url: "../../pages/login/login?flag=center"
                        })
                    }else{
                        that.setData({
                            is_authorization:true
                        })
                    }
                } else {
                  that.setData({
                    flag: false
                  })
                }
            }
        })
    },//获取验证码
    clickVerify: function () {
        var that = this;
        var phone_number = that.data.phone;
        if (phone_number) {
            if (!app.phone_number_reg(phone_number)) {
                wx.showToast({
                    title: '请输入正确的手机号',
                    icon: "loading"
                })
            } else {
                // 将获取验证码按钮隐藏60s，60s后再次显示
                that.setData({
                    is_show: !that.data.is_show   //false
                })
                settime(that);
                // 发送验证码
                var params = {};
                params['phone_number'] = phone_number;
                wx.request({
                    url: app.config() + "member/xiaoSendCode",
                    data: params,
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        // 操作太过于频繁tips
                        if (res.data.status == 4) {
                            wx.showToast({
                                title: '请稍后重试',
                                imagg: "../../images/warning.png"
                            })
                        }
                    }
                })
            }
        } else {
            wx.showToast({
                title: '请输入手机号',
                icon: "loading"
            })
        }
    },//手机号码
    member_phone: function (e) {
        var that = this;
        var phone = e.detail.value;
        that.setData({
            phone: phone
        })
    },//验证码
    verification_code: function (e) {
        var that = this;
        var code = e.detail.value;
        that.setData({
            code: code
        })
    },//提交登陆
    submit: function () {
        var that = this;
        var phone_number = that.data.phone;
        var code = that.data.code;
        var open_id = that.data.memberInfo.open_id;
        if (!phone_number) {
            return;
        }
        if (!app.phone_number_reg(phone_number)) {
            return;
        }
        if (!code) {
            wx.showToast({
                title: '请输入验证码',
                image: "../../images/warning.png"
            })
            return;
        }
        var params = {};
        params['phone_number'] = phone_number;
        params['verify_code'] = code;
        params['open_id'] = open_id;
        wx.request({
            url: app.config() + "member/xiaoLogANDReg",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.data.status == 0) {
                    var data = res.data.data;
                    wx.showToast({
                        title: '验证成功',
                        icon: 'success'
                    })
                    wx.setStorage({
                        key: 'memberInfo',
                        data: data,
                    })
                    that.setData({
                        is_authorization:true
                    })
                    that.init();
                } else {
                    wx.showToast({
                        title: res.data.message,
                        image: "../../images/warning.png"
                    })
                }
            }
        })
    },
  myevent(e) {
    this.setData({
      flag: e.detail.flag,
    })
    this.init();
  },
})


var countdown = 60;
var settime = function (that) {
    if (countdown == 0) {
        that.setData({
            is_show: true,
            last_time: ""
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })
        countdown--;
    }
    setTimeout(function () {
        settime(that)
    }, 1000)
}